package client;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.text.SimpleDateFormat;
import java.util.Date;


/**
 * @author lizuhu
 * @description
 * @date 2021/9/6 17:06
 */
class ClientView extends JFrame {
    private ObjectOutputStream oos;
    private ObjectInputStream ois;
    private Socket socket;
    //时间显示格式
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    //窗口宽度
    final int WIDTH = 600;
    //窗口高度
    final int HEIGHT = 700;

    //创建发送按钮
    JButton btnSend = new JButton("发送");
    //创建清除按钮
    JButton btnClear = new JButton("清屏");
    //创建退出按钮
    JButton btnExit = new JButton("退出");


    //创建文本输入框, 参数分别为行数和列数
    JTextArea jtaSay = new JTextArea();
    //创建聊天消息框
    JTextArea jtaChat = new JTextArea();

    //创建聊天消息框的滚动窗
    JScrollPane jspChat = new JScrollPane(jtaChat);


    //设置默认窗口属性，连接窗口组件
    public ClientView(Socket socket,String name) {
        //标题
        setTitle("聊天室");
        //大小
        setSize(WIDTH, HEIGHT);
        //不可缩放
        setResizable(false);
        //设置布局:不适用默认布局，完全自定义
        setLayout(null);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //获取本机屏幕横向分辨率
        int w = Toolkit.getDefaultToolkit().getScreenSize().width;
        //获取本机屏幕纵向分辨率
        int h = Toolkit.getDefaultToolkit().getScreenSize().height;
        //将窗口置中
        setLocation((w - this.WIDTH) / 2, (h - this.HEIGHT) / 2);
        this.setResizable(false);  //窗体大小设置为不可改
        this.setVisible(true);
        //设置按钮大小和位置
        btnSend.setBounds(260, 600, 100, 50);
        btnClear.setBounds(380, 600, 100, 50);
        btnExit.setBounds(490, 600, 100, 50);

        //设置按钮文本的字体
        btnSend.setFont(new Font("楷体", Font.BOLD, 18));
        btnClear.setFont(new Font("楷体", Font.BOLD, 18));
        btnExit.setFont(new Font("楷体", Font.BOLD, 18));
        //添加按钮
        this.add(btnSend);
        this.add(btnClear);
        this.add(btnExit);

        //设置文本输入框大小和位置
        jtaSay.setBounds(20, 460, 560, 120);
        //设置文本输入框字体
        jtaSay.setFont(new Font("楷体", Font.BOLD, 18));
        jtaSay.setFont(new Font("楷体", Font.BOLD, 18));

        //添加文本输入框
        this.add(jtaSay);

        //聊天消息框自动换行
        jtaChat.setLineWrap(true);
        //聊天框不可编辑，只用来显示
        jtaChat.setEditable(false);
        //设置聊天框字体
        jtaChat.setFont(new Font("楷体", Font.BOLD, 18));

        //设置滚动窗的水平滚动条属性:不出现
        jspChat.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        //设置滚动窗的垂直滚动条属性:需要时自动出现
        jspChat.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
        //设置滚动窗大小和位置
        jspChat.setBounds(20, 20, 560, 400);
        //添加聊天窗口的滚动窗
        this.add(jspChat);

        try {
            this.socket = socket;
            oos = new ObjectOutputStream(socket.getOutputStream());
            oos.writeObject(name);
            ois = new ObjectInputStream(socket.getInputStream());
            //接收服务端消息线程
            new Thread(new ClientGet(ois)).start();
        } catch (Exception e) {
            System.out.println(e);
        }
        //添加发送按钮的响应事件
        btnSend.addActionListener
                (
                        new ActionListener() {
                            @Override
                            public void actionPerformed(ActionEvent event) {
                                try {
                                    //向服务器发送聊天信息
                                    oos.writeObject((jtaSay.getText()));
                                    jtaChat.append(jtaSay.getText() + "\n");
                                } catch (Exception e) {
                                } finally {
                                    //文本输入框清除
                                    jtaSay.setText("");
                                }
                            }
                        }
                );
        //添加清屏按钮的响应事件
        btnClear.addActionListener
                (
                        new ActionListener() {
                            @Override
                            public void actionPerformed(ActionEvent event) {
                                //聊天框清屏
                                jtaChat.setText("");
                            }
                        }
                );
        //添加退出按钮的响应事件
        btnExit.addActionListener
                (
                        new ActionListener() {
                            @Override
                            public void actionPerformed(ActionEvent event) {
                                try {
                                    //向服务器发送退出信息
                                    oos.writeObject("bye");
                                    //退出
                                    System.exit(0);
                                } catch (Exception e) {
                                }
                            }
                        }
                );

    }

    class ClientGet implements Runnable {
        private ObjectInputStream ois;

        public ClientGet(ObjectInputStream ois) {
            this.ois = ois;
        }

        @Override
        public void run() {
            try {
                while (true) {
                    String msg = (String) ois.readObject();
                    System.out.println(msg);
                    jtaChat.append(sdf.format(new Date()) + "\n" + msg + "\n"+"-------------------------------------------------\n");
                }
            } catch (Exception e) {
                System.out.println("服务器出现错误！");
                jtaChat.append("服务器出现错误！请退出重试！");
            }
        }
    }

}
